#include "soundmanager.h"

#include <qdebug.h>
#include "exceptions.h"

using namespace std;
using namespace sf;

SoundManager::SoundManager() : m_sounds()
{
}

SoundManager::~SoundManager()
{
}

SoundManager& SoundManager::getInstance()
{
    static SoundManager instance;
    return instance;
}

//DEBUGGING
void SoundManager::printPaths() const
{
    qDebug() << "SoundManager's loaded sounds :";

    QMap<QString, sf::SoundBuffer>::const_iterator i = m_sounds.constBegin();
    while(i != m_sounds.constEnd())
    {
        qDebug() << i.key();
        ++i;
    }
}

const sf::SoundBuffer& SoundManager::getSoundBuffer(const QString& soundPath)
{
    QMap<QString, sf::SoundBuffer>::iterator i = m_sounds.find(soundPath);
    if(i != m_sounds.end()) //the requested sound already exists
    {
        return i.value();
    }
    else	//the requested sound hasn't been loaded yet
    {
        //creates the sound using the given path
        i = m_sounds.insert(soundPath, sf::SoundBuffer());

        if(i.value().LoadFromFile(soundPath.toStdString()))
        {
            return i.value();
        }
        else
        {
            throw EX_FILE_NOT_FOUND(soundPath);
        }
    }
}
